package ce.com.newbluedemo.k2;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import ce.com.cenewbluesdk.entity.k2.K2_SendUserInfo;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/27.
 */

public class ActivitySendUserInfo extends Activity implements View.OnClickListener,
        View.OnFocusChangeListener, RadioGroup.OnCheckedChangeListener {
    private EditText mEtUserID;
    private EditText mEtHeight;
    private EditText mEtAge;
    private Button mSendUserInfo;
    private RadioGroup mRgGender;
    private RadioGroup mRgHandSide;
    private final int MALE = 0;
    private final int FEMALE = 1;
    private final int LEFT_HAND = 0;
    private final int RIGHT_HAND = 1;
    private int mGender = MALE;
    private int mHandSide = RIGHT_HAND;
    private int Height = 0;
    private int Age = 0;
    private int Weight = 0;
    private int mUserId = 0;
    private CEDevK2Proxy mK2Proxy;

    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("发送失败，请稍后重试！");
            } else {
                mEtHeight.setText("");
                mEtUserID.setText("");
                mEtAge.setText("");
                mRgHandSide.clearCheck();
                mRgGender.clearCheck();
                showToast("发送成功！");
            }
        }
    };

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_user_info);
        mEtUserID = (EditText) findViewById(R.id.et_user_id);
        mEtHeight = (EditText) findViewById(R.id.et_height);
        mEtAge = (EditText) findViewById(R.id.et_age);
        mRgGender = (RadioGroup) findViewById(R.id.rg_gender);
        mRgHandSide = (RadioGroup) findViewById(R.id.rg_hand_side);
        mSendUserInfo = (Button) findViewById(R.id.btn_send_user_info);
        mEtUserID.setOnFocusChangeListener(this);
        mEtHeight.setOnFocusChangeListener(this);
        mEtAge.setOnFocusChangeListener(this);
        mSendUserInfo.setOnClickListener(this);
        mRgGender.setOnCheckedChangeListener(this);
        mRgHandSide.setOnCheckedChangeListener(this);
        mK2Proxy = BleFactory.getInstance().getK2Proxy();
        mK2Proxy.setHandler(mK2Handler);

    }


    @Override
    public void onClick(View v) {
        if (mK2Proxy.isDisconnect()) {
            toast(this, "蓝牙已断开，请重新连接");
            return;
        }
        switch (v.getId()) {
            case R.id.btn_send_user_info:
                if (mUserId != 0 && Age != 0 && Height != 0 && Weight != 0) {
                    mK2Proxy.getSendHelper().sendUserInfo(new K2_SendUserInfo(mUserId, mGender, Age, Height, Weight, mHandSide));
                } else {
                    toast(this, "您的数据不合法，请重新检查输入");
                }

                break;

            default:
                break;

        }

    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) return;
        switch (v.getId()) {
            case R.id.et_user_id:
                if (!hasFocus) {
                    String input = ((EditText) v).getText().toString();
                    if (isInteger(input)) {
                        mUserId = Integer.parseInt(input);
                    } else {
                        toast(this, "抱歉，您输入的用户ID不合法");
                    }
                }
                break;


            case R.id.et_age:
                if (!hasFocus) {
                    String input = ((EditText) v).getText().toString();
                    if (checkNum(input, 10, 100)) {
                        Age = Integer.parseInt(input);
                    } else {
                        toast(this, "抱歉，您输入的年龄不合法");
                    }
                }
                break;

            case R.id.et_height:
                if (!hasFocus) {
                    String input = ((EditText) v).getText().toString();
                    if (checkNum(input, 80, 280)) {
                        Height = Integer.parseInt(input);
                    } else {
                        toast(this, "抱歉您输入的身高不合法");
                    }
                }
                break;
            case R.id.et_weight:
                if (!hasFocus) {
                    String input = ((EditText) v).getText().toString();
                    if (checkNum(input, 30, 200)) {
                        Weight = Integer.parseInt(input);
                    } else {
                        toast(this, "抱歉您输入的体重不合法");
                    }
                }
                break;

            default:
                break;
        }
    }

    private void toast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    private boolean checkNum(String input, int min, int max) {
        if (isInteger(input)) {
            int num = Integer.parseInt(input);
            return !(num > max || num < min);
        }
        return false;
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_hand_side:
                mHandSide = checkedId == R.id.rb_left_hand ? LEFT_HAND : RIGHT_HAND;
                break;
            case R.id.rg_gender:
                mGender = checkedId == R.id.rb_male ? MALE : FEMALE;
                break;
            default:
                break;
        }
    }

    private boolean isInteger(String input) {
        try {
            Integer.valueOf(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }

    }
}
